﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using EasyKindle.Mail;

namespace EasyKindle
{
    class Kindle
    {
        private static PostInfo Info;
        public static void SetPostInfo(PostInfo postInfo)
        {
            Info = postInfo;
        }
        public static bool Send()
        {
            if (Info == null) return false;
            Email email = new Email();
            email.IsbodyHtml = true;
            email.attachmentsPath = new string[] { Info.BookPath };
            email.MailFrom = Info.PostAdress;
            email.MailPwd = Info.PostPassWord;
            email.MailToArray = new string[] { Info.KindleAdress };
            email.MailSubject = "EasyKindle图书推送";
            var smtp = "smtp." + Info.PostAdress.Split('@')[1];
            email.Host = smtp;
            var result = email.Send();
            return result;
        }
        public static bool Send(PostInfo info)
        {
            SetPostInfo(info);
            return Send();
        }

        public static bool InvadidateKindleAdress(string adress)
        {
            return Regex.IsMatch(adress, ".*@kindle.cn");
        }

        public static bool InvadidateEmailAdress(string adress)
        {
            return Regex.IsMatch(adress, @".*@.*\.com");
        }

        public static string FileFilterString
        {
            get
            {
                return "所有亚马逊支持的文件|*.mobi;*.azw;*.pdf;*.doc;*.docx;*.html;*.htm;*.rtf;*.txt;*.jpeg;*.jpg;*.gif;*.png;*.bmp";
            }
        }

        public class PostInfo
        {
            /// <summary>
            /// 已认可的发件邮箱
            /// </summary>
            public string PostAdress { get; set; }
            /// <summary>
            /// 发件邮箱密码
            /// </summary>
            public string PostPassWord { get; set; }
            /// <summary>
            /// Kindle推送邮箱账号
            /// </summary>
            public string KindleAdress { get; set; }
            /// <summary>
            /// 推送电子书路径
            /// </summary>
            public string BookPath { get; set; }
        }
    }
}
